package com.ruoyi.system.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.exception.BaseException;
import com.ruoyi.common.core.plus.web.controller.BasePlusController;
import com.ruoyi.common.core.plus.web.page.PageDto;
import com.ruoyi.common.core.utils.ConvertUtil;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.system.domain.LanguageKey;
import com.ruoyi.system.domain.LanguageType;
import com.ruoyi.system.domain.LanguageValue;
import com.ruoyi.system.domain.params.LanguageValueParams;
import com.ruoyi.system.domain.vo.LanguageValueExportVo;
import com.ruoyi.system.domain.vo.LanguageValueVo;
import com.ruoyi.system.service.ILanguageKeyService;
import com.ruoyi.system.service.ILanguageTypeService;
import com.ruoyi.system.service.ILanguageValueService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 语言键值Controller
 *
 * @author huyafang
 * @date 2023-03-06
 */
@RestController
@RequestMapping("/system/manage/language_value")
@Api(tags = "语言键值")
public class LanguageValueController extends BasePlusController {
    @Autowired
    private ILanguageValueService languageValueService;

    @Autowired
    private ILanguageTypeService languageTypeService;

    @Autowired
    private ILanguageKeyService languageKeyService;

    /**
     * 查询语言键值列表
     */
    @PreAuthorize("@ss.hasPermi('coin-trade:common:language_value:list')")
    @GetMapping("/list")
    @ApiOperation(value = "分页")
    public TableDataInfo<LanguageValueVo> list(LanguageValueParams languageValueConditionDto) {
        PageDto pageDto = startPage();
        Page<LanguageValueVo> page = new Page<>(pageDto.getCurrentPage(), pageDto.getPageSize());
        IPage<LanguageValueVo> list = languageValueService.queryList(page, languageValueConditionDto);
        return getDataTable(list);
    }

    /**
     * 导出语言键值列表
     */
    @PreAuthorize("@ss.hasPermi('coin-trade:common:language_value:export')")
    @Log(title = "语言键值", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出")
    public void export(HttpServletResponse response, LanguageValueParams languageValueParams) throws IOException {
        Page<LanguageValueVo> page = new Page<>(1, -1);
        IPage<LanguageValueVo> languageValueVos = languageValueService.queryList(page, languageValueParams);
        List<LanguageValueExportVo> languageValueExportVos = ConvertUtil.sourceToTarget(languageValueVos.getRecords(), LanguageValueExportVo.class);
        ExcelUtil<LanguageValueExportVo> util = new ExcelUtil<>(LanguageValueExportVo.class);
        util.exportExcel(response, languageValueExportVos, "languageValue");
    }

    /**
     * 获取语言键值详细信息
     */
    @PreAuthorize("@ss.hasPermi('coin-trade:common:language_value:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "查看")
    public R<LanguageValueVo> getInfo(@PathVariable("id") Long id) {
        LanguageValue languageValue = languageValueService.getById(id);
        LanguageKey languageKey = languageKeyService.getByCode(languageValue.getCode());
        LanguageValueVo languageValueVo = ConvertUtil.sourceToTarget(languageValue, LanguageValueVo.class);
        languageValueVo.setKeyType(languageKey.getType());
        return R.ok(languageValueVo);
    }

    /**
     * 新增语言键值
     */
    @PreAuthorize("@ss.hasPermi('coin-trade:common:language_value:add')")
    @Log(title = "语言键值", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增")
    public R add(@RequestBody LanguageValue languageValue) {
        return R.ok(languageValueService.save(languageValue));
    }

    /**
     * 修改语言键值
     */
    @PreAuthorize("@ss.hasPermi('coin-trade:common:language_value:edit')")
    @Log(title = "语言键值", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改")
    public R edit(@RequestBody LanguageValue languageValue) {
        return R.ok(languageValueService.update(languageValue));
    }

    /**
     * 删除语言键值
     */
    @PreAuthorize("@ss.hasPermi('coin-trade:common:language_value:remove')")
    @Log(title = "语言键值", businessType = BusinessType.DELETE)
    @DeleteMapping("/{codes}")
    @ApiOperation(value = "删除")
    public R remove(@PathVariable String[] codes) {
        languageValueService.deleteByKey(codes);
        return R.ok();
    }


    /**
     * 导出模板
     */
    @PreAuthorize("@ss.hasPermi('coin-trade:common:language_value:exportModel')")
    @Log(title = "导出模板", businessType = BusinessType.EXPORT)
    @PostMapping("/exportModel")
    @ApiOperation(value = "导出模板")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "typeCode", value = "语言code", paramType = "query", dataType = "String")
    })
    public void exportModel(HttpServletResponse response, String typeCode) throws IOException {
        String sheetName = typeCode + "LanguageValue";
        LanguageType languageType = languageTypeService.queryLanguageTypeByCode(typeCode);
        if (ObjectUtil.isNull(languageType)) {
            throw new BaseException("语言不存在");
        }
       /* List<LanguageValue> byList = manageLanguageValueService.getByList(Wrappers.lambdaQuery(LanguageValue.class).eq(LanguageValue::getTypeCode, typeCode));
        List<LanguageValueExportVo> languageValueVoList = ConvertUtil.sourceToTarget(byList, LanguageValueExportVo.class);*/
        ExcelUtil<LanguageValueExportVo> util = new ExcelUtil<>(LanguageValueExportVo.class);
        util.exportExcel(response, null, sheetName);
    }

    @Log(title = "导入", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('coin-trade:common:language_value:exportModel')")
    @PostMapping("/importData")
    @ApiOperation(value = "导入")
    public R importData(MultipartFile file) throws Exception {
        ExcelUtil<LanguageValueExportVo> util = new ExcelUtil<>(LanguageValueExportVo.class);
        List<LanguageValueExportVo> languageValueVoList = util.importExcel(file.getInputStream());
        List<LanguageValueVo> languageValueVoList1 = ConvertUtil.sourceToTarget(languageValueVoList, LanguageValueVo.class);
        languageValueService.addUpdateData(languageValueVoList1);
        return R.ok();
    }

    /**
     * 批量新增修改
     */
    @PreAuthorize("@ss.hasPermi('coin-trade:common:language_value:addUpdate')")
    @Log(title = "语言键值", businessType = BusinessType.INSERT)
    @PostMapping("/addList")
    @ApiOperation(value = "批量新增修改")
    public R addList(@RequestBody List<LanguageValueParams> languageValue) {
        List<LanguageValueVo> languageValueVoList = ConvertUtil.sourceToTarget(languageValue, LanguageValueVo.class);
        languageValueService.addUpdateData(languageValueVoList);
        return R.ok();
    }

}
